/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.modules.PCguanli.entity.DriversDatcherEntity;
import io.renren.modules.PCguanli.entity.LineDeptEntity;
import io.renren.modules.PCguanli.entity.LineEntity;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.dao.UserRegistrationDao;
import io.renren.modules.app.entity.Response;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.entity.UserRegistrationEntity;
import io.renren.modules.app.entity.UserWithCarNumber;
import io.renren.modules.app.service.UserService;
import io.renren.modules.app.user.UserPicUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public UserEntity queryByOpenid(String openId) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("open_id", openId));
	}

	@Override
	public UserEntity queryByIphone(String iPhone) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("iPhone", iPhone));
	}
	@Override
	public UserEntity queryByuserID(int userId) {
		UserEntity userEntity = userDao.findByUserId(userId);
		if (userEntity == null) {
			throw new RRException("用户不存在");
		}
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("user_id", userId));
	}

	@Override
	public UserEntity updateUserPic(UserPicUpdateVO userPicUpdate) {
		return updateDriversDatcherWithPictures(userPicUpdate.getUserId(), userPicUpdate.getPicture1(), userPicUpdate.getPicture2(), userPicUpdate.getPicture3(), userPicUpdate.getPicture4());
	}
	@Transactional
	public UserEntity updateDriversDatcherWithPictures(int userId, String picture1, String picture2, String picture3, String picture4) {
		// 此处应根据你的Dao接口中提供的方法进行调整
		UserEntity driversDatcherEntity = userDao.findByUserId(userId);

		if (driversDatcherEntity != null) {
			driversDatcherEntity.setPicture1(picture1);
			driversDatcherEntity.setPicture2(picture2);
			driversDatcherEntity.setPicture3(picture3);
			driversDatcherEntity.setPicture4(picture4);

			userDao.updateById(driversDatcherEntity);
		} else {
			throw new RuntimeException("User not found with id " + userId);
		}

		return driversDatcherEntity;
	}
	@Autowired
	private UserRegistrationDao userRegistrationDao;

	@Override
	public UserEntity updateUserInfo(int userId, String name,String iPhone, String ID ,
											int classification, String carNumber, int sex ) {
		UserEntity userEntity = userDao.findByUserId(userId);
		if (userEntity == null) {
			throw new RRException("用户不存在");
		}
		userEntity.setName(name);
		userEntity.setIPhone(iPhone);
		userEntity.setID(ID);
		userEntity.setSex(sex);
		userEntity.setClassification(classification);
		userEntity.setCarnumber(carNumber);
		try{
		userDao.updateById(userEntity);
		}catch (RRException e) {
			// Handle the exception
			throw new RRException("身份证已有记录，无法修改");
		}

		return userEntity;
	}
	@Override
	public UserEntity updateDatcherr(int userId, String name,String iPhone, String ID ,
											int classification, int sex ) {
		UserEntity userEntity = userDao.findByUserId(userId);
		if (userEntity == null) {
			throw new RRException("用户不存在");
		}
		userEntity.setName(name);
		userEntity.setIPhone(iPhone);
		userEntity.setID(ID);
		userEntity.setSex(sex);
		userEntity.setClassification(classification);

		try{
			userDao.updateById(userEntity);
		}catch (RRException e) {
			// Handle the exception
			throw new RRException("身份证已有记录，无法修改");
		}
/*		UserEntity user = userDao.selectOne(new LambdaQueryWrapper<UserEntity>()
						.eq(UserEntity::getName, drivername)
						.eq(UserEntity::getIPhone,driveriphone)
				        .eq(UserEntity::getClassification, 1));
		if(user == null){
			throw new RRException("资料有误或司机尚未填写好个人资料");
		}else {

			userEntity.setDriverId(user.getUser_id());
			userEntity.setCardealer(user.getCardealer());
			userEntity.setDrivernumber(user.getCarnumber());
			userDao.updateById(userEntity);
			// Set other fields of newUserRegistrationEntity as needed
		}*/
		return userEntity;
	}


}

